package cn.lh.org.controller;

import cn.lh.basic.util.ExcelUtils;
import cn.lh.basic.util.JsonResult;
import cn.lh.basic.util.Page;
import cn.lh.org.domain.Employee;
import cn.lh.org.domain.Shop;
import cn.lh.org.domain.ShopAuditLog;
import cn.lh.org.dto.ShopDto;
import cn.lh.org.exception.BusinessException;
import cn.lh.org.query.ShopQuery;
import cn.lh.org.service.IShopAuditLogService;
import cn.lh.org.service.IShopService;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RequestMapping("/shop")
@RestController
public class ShopController {
    @Autowired
    private IShopService shopService;


    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id") long id) {
        return shopService.findById(id);
    }

    @GetMapping
    public List<Shop> findAll() {
        return shopService.findAll();
    }

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") long id) {
        try {
            shopService.del(id);
            return JsonResult.set(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, "删除失败");
        }

    }

    @PutMapping
    public JsonResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() == null) {
                shopService.add(shop);
            } else {
                shopService.update(shop);
            }
            return JsonResult.set(true, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, "操作失败");
        }

    }

    @PostMapping
    public Page<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }

    @PatchMapping
    public JsonResult batchDelete(@RequestBody Long[] ids){
        try {
            shopService.batchDelete(ids);
            return JsonResult.set(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, "删除失败");
        }
    }

    @PostMapping("/settlement")
    public JsonResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return JsonResult.set(true, "注册成功");
        } catch (BusinessException e) {
            return JsonResult.set(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false,"系统错误，稍后重试");
        }
    }


    @PostMapping("/audit/pass")
    public JsonResult pass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.pass(shopAuditLog);
            return JsonResult.set(true, "审核通过");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, "审核失败");
        }
    }

    @PostMapping("/audit/reject")
    public JsonResult reject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.reject(shopAuditLog);
            return JsonResult.set(true, "驳回成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, "驳回失败");
        }
    }

    @GetMapping("/export")
    public void excelExport(HttpServletResponse response){
        List<Shop> shops = shopService.findAll();
        ExcelUtils.exportExcel(shops,"店铺信息表", "页面一", Shop.class, "店铺.xls", response);
    }

    @PostMapping("/import")
    public void excelImport(MultipartFile file){
        List<Shop> shops = ExcelUtils.importExcel(file, 1, 2, Shop.class);
        shops.forEach(System.out::println);
    }

    @GetMapping("/echarts")
    public List<ShopDto> echartsData(){
        return shopService.getCountByState();
    }
}
